package com.mobile.tools.cfg;

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;

import com.mobile.tools.XMLUtil;

/**
 * hibernate配置文件管理工具类
 * @author huangfeng
 *
 */
public class HibernateConfigManagerUtil {	
	/**
	 * 更新hibernate的配置文件
	 * @param configName	配置文件名
	 * @param mappingName	映射名称
	 * @param resource		hbm.xml文件路径
	 * 
	 */
	public static void updateHibernateConfig(String configName,String resource){
		String file=Thread.currentThread().getContextClassLoader().getResource(configName).getPath();
		try {
			Document document=XMLUtil.loadDocument(file);
			NodeList nodeList=document.getElementsByTagName("session-factory");
			Element element=(Element)nodeList.item(0);
			Element elementNew=document.createElement("mapping");
			elementNew.setAttribute("resource",resource);
			Text text=document.createTextNode("");
			element.appendChild(text);
			element.appendChild(elementNew);
			XMLUtil.saveDocument(document, file);
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TransformerException e){
			e.printStackTrace();
		}
	}
}
